//
//  VC2.swift
//  测试TextField
//
//  Created by shanlin on 2017/11/28.
//  Copyright © 2017年 shanlin. All rights reserved.
//  http://www.jianshu.com/p/230db7b6dc02

import UIKit
import XJUIKit
import XJExtension

class VC2: BaseVC {

    fileprivate lazy var tf3: SLTextField = SLTextField(frame: CGRect.zero, style: SLTextFieldStyle())
    fileprivate lazy var tf4: SLTextField = SLTextField(frame: CGRect.zero, style: SLTextFieldStyle())
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationItem.title = "SLTextField全新整合"
        setupUI()
    }
    
    deinit {
        print("\(self.classForCoder)类执行了\(#function)方法的打印了...")
        // FIXME: 仅短信验证码需要执行deini方法，其他不需要 
        self.tf3.commitTransaction(isSuccess: false)
        
    }
}

extension VC2 {
    fileprivate func setupUI() {
        
        
        let style_01 = SLTextFieldStyle()
        style_01.limitLength = 5
        let textField_01 = SLTextField(frame: CGRect.zero, style: style_01)
        textField_01.frame = CGRect(x: 20, y: 20, width: UIScreen.main.bounds.width - 40, height: 60)
        textField_01.backgroundColor = UIColor.white
        view.addSubview(textField_01)
        textField_01.textFieldCallBack = { [weak self] (text, limitMsg) in
            guard let _ = self else { return }
            print("text=\(text), limitMsg=\(limitMsg)")
            if limitMsg.count != 0 {
                self?.hudText(limitMsg)
            }
        }


        let style_02 = SLTextFieldStyle()
        style_02.leftStyle = .leftText
        style_02.leftLabelText = "账户:"
        let textField_02 = SLTextField(frame: CGRect.zero, style: style_02)
        textField_02.frame = CGRect(x: 20, y: textField_01.frame.maxY + 20, width: UIScreen.main.bounds.width - 40, height: 60)
        textField_02.backgroundColor = UIColor.white
        view.addSubview(textField_02)
        textField_02.textFieldCallBack = { [weak self] (text, limitMsg) in
            guard let _ = self else { return }
            print("text02=\(text), limitMsg02=\(limitMsg)")
        }
        
        
        let style_03 = SLTextFieldStyle()
        style_03.leftStyle = .leftImage
        style_03.rightStyle = .rightText
        let textField_03 = SLTextField(frame: CGRect.zero, style: style_03)
        self.tf3 = textField_03
        style_03.isDeinitPrint = true
        textField_03.frame = CGRect(x: textField_02.frame.maxY + 20, y:  20, width: UIScreen.main.bounds.width - 40, height: 60)
        textField_03.backgroundColor = UIColor.white
        view.addSubview(textField_03)
        textField_03.textFieldCodeCallBack = { [weak self] (explain, btn) in
            guard let _ = self else { return }
            print("explain=\(explain)")
            
            // FIXME: 在回调里不能使用局部变量去调用方法，需要使用属性变量去调用、防止view层deinit方法不执行！！！
            // FIXME: 只有短信验证码需要在两处执行这个方法： 成功获取验证码即将跳转下一个页面之前commitTransaction(isSuccess: true)； deinit方法里执行 commitTransaction(isSuccess: false)、不然定时器不设置无效、也不执行view层deinit()
            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 10, execute: {
                self?.tf3.commitTransaction(isSuccess: true)
            })
        }
        textField_03.textFieldCallBack = { [weak self] (text, limitMsg) in
            guard let _ = self else { return }
            print("text03=\(text), limitMsg03=\(limitMsg)")
        }
        
        
        
        let style_04 = SLTextFieldStyle()
        style_04.leftStyle = .leftImage
        style_04.rightStyle = .rightImage
        style_04.isGraphicsCode = true
        let textField_04 = SLTextField(frame: CGRect.zero, style: style_04)
        self.tf4 = textField_04
        textField_04.frame = CGRect(x: 20, y: textField_03.frame.maxY + 20, width: UIScreen.main.bounds.width - 40, height: 60)
        textField_04.backgroundColor = UIColor.white
        view.addSubview(textField_04)
        textField_04.textFieldCodeCallBack = { [weak self] (explain, btn) in
            guard let _ = self else { return }
            print("explain=\(explain)")
            // FIXME:在回调里不能使用局部变量去调用方法，需要使用属性变量去调用、防止view层deinit方法不执行！！！
            // 别使用textField_04.commitTransaction(isSuccess: true) 请使用self.tf4.commitTransaction(isSuccess: true)去调用
            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 5, execute: {
                self?.tf4.commitTransaction(isSuccess: true)
                btn.setImage(UIImage(named: "inputField_imageDemo"), for: .normal)
            })
        }



        let style_05 = SLTextFieldStyle()
        style_05.leftStyle = .leftText
        style_05.rightStyle = .rightImage
        style_05.leftLabelText = "密码:"
        let textField_05 = SLTextField(frame: CGRect.zero, style: style_05)
        textField_05.frame = CGRect(x: 20, y: textField_04.frame.maxY + 20, width: UIScreen.main.bounds.width - 40, height: 60)
        textField_05.backgroundColor = UIColor.white
        view.addSubview(textField_05)



        let style_06 = SLTextFieldStyle()
        style_06.leftStyle = .leftText
        style_06.rightStyle = .rightImage
        style_06.leftLabelText = ""
        let textField_06 = SLTextField(frame: CGRect.zero, style: style_06)
        textField_06.frame = CGRect(x: 20, y: textField_05.frame.maxY + 20, width: UIScreen.main.bounds.width - 40, height: 60)
        textField_06.backgroundColor = UIColor.white
        view.addSubview(textField_06)


        let style_07 = SLTextFieldStyle()
        style_07.leftStyle = .leftImage
        style_07.rightStyle = .rightImage
        style_07.leftIcon = UIImage()
        style_07.rightImageEyeNormal = UIImage()
        let textField_07 = SLTextField(frame: CGRect.zero, style: style_07)
        textField_07.frame = CGRect(x: 20, y: textField_06.frame.maxY + 20, width: UIScreen.main.bounds.width - 40, height: 60)
        textField_07.backgroundColor = UIColor.white
        view.addSubview(textField_07)

        
        let style_08 = SLTextFieldStyle()
        style_08.leftStyle = .leftImage
        style_08.rightStyle = .rightImage
        style_08.leftIcon = UIImage()
        style_08.rightImageEyeNormal = UIImage()
        style_08.rightCustomView = UIView()
        let textField_08 = SLTextField(frame: CGRect.zero, style: style_08)
        textField_08.frame = CGRect(x: 20, y: textField_07.frame.maxY + 20, width: UIScreen.main.bounds.width - 40, height: 60)
        textField_08.backgroundColor = UIColor.white
        view.addSubview(textField_08)
        
        let style_09 = SLTextFieldStyle()
        style_09.leftStyle = .leftImage
        style_09.rightStyle = .rightImage
        style_09.leftIcon = UIImage()
        style_09.rightImageEyeNormal = UIImage()
        style_09.rightCustomView = UIView()
        let textField_09 = SLTextField(frame: CGRect.zero, style: style_09)
        textField_09.frame = CGRect(x: 20, y: textField_08.frame.maxY + 20, width: UIScreen.main.bounds.width - 40, height: 60)
        textField_09.backgroundColor = UIColor.white
        textField_09.layer.cornerRadius = textField_09.height * 0.5
        view.addSubview(textField_09)
        
        let style_10 = SLTextFieldStyle()
        style_10.leftStyle = .leftText
        style_10.rightStyle = .rightImage
        style_10.leftBgViewBackgroundColor = UIColor(white: 244/255, alpha: 1.0)
        style_10.rightImageEyeNormal = UIImage()
        style_10.rightCustomView = UIView()
        let textField_10 = SLTextField(frame: CGRect.zero, style: style_10)
        textField_10.frame = CGRect(x: 20, y: textField_09.frame.maxY + 20, width: UIScreen.main.bounds.width - 40, height: 50)
        textField_10.backgroundColor = UIColor.white
        textField_10.layer.cornerRadius = textField_10.height * 0.5
        view.addSubview(textField_10)
        
        let style_11 = SLTextFieldStyle()
        style_11.leftStyle = .leftImage
        style_11.rightStyle = .rightImage
        style_11.rightImageEyeNormal = UIImage()
        style_11.rightCustomView = UIView()
        let textField_11 = SLTextField(frame: CGRect.zero, style: style_11)
        textField_11.frame = CGRect(x: 20, y: textField_10.frame.maxY + 20, width: UIScreen.main.bounds.width - 40, height: 42)
        textField_11.backgroundColor = UIColor.white
        textField_11.layer.cornerRadius = textField_11.height * 0.5
        view.addSubview(textField_11)
        
        let style_12 = SLTextFieldStyle()
        style_12.leftStyle = .leftImage
        style_12.rightStyle = .rightImage
        style_12.leftIcon = UIImage()
        style_12.rightImageEyeNormal = UIImage()
        style_12.rightCustomView = UIView()
        let textField_12 = SLTextField(frame: CGRect.zero, style: style_12)
        textField_12.frame = CGRect(x: 20, y: textField_11.frame.maxY + 20, width: UIScreen.main.bounds.width - 40, height: 42)
        textField_12.backgroundColor = UIColor.white
        textField_12.layer.cornerRadius = textField_12.height * 0.5
        view.addSubview(textField_12)
    }
}
